package com.demohomework.homeworkdemo.service.impl;

import com.demohomework.homeworkdemo.mapper.StudentMapper;
import com.demohomework.homeworkdemo.pojo.Student;
import com.demohomework.homeworkdemo.service.StudentService;
import com.demohomework.homeworkdemo.util.Md5Util;
import com.demohomework.homeworkdemo.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    //查询
    @Override
    public Student findbyUsername(String username) {
        Student s = studentMapper.findbyUsername(username);
        return s;
    }

    //注册
    @Override
    public void register(String username, String password) {
        //加密
        String md5Password  = Md5Util.string2MD5(password);
        //添加
        studentMapper.add(username, md5Password);
    }
    //更新信息
    @Override
    public void update(Student student) {
        studentMapper.update(student);
    }

    //更新密码
    @Override
    public void updatePwd(String newPwd) {
        //加密
        String md5Password  = Md5Util.string2MD5(newPwd);
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        //修改
        studentMapper.updatePwd(md5Password,id);
    }

    @Override
    public List<Student> list() {
        return studentMapper.list();
    }

    @Override
    public void delete(Integer id) {
        studentMapper.delete(id);
    }

    @Override
    public void add(Student student) {
        String defaultPassword = "123456";
        String md5Password  = Md5Util.string2MD5(defaultPassword);
        student.setPassword(md5Password);
        studentMapper.stuAdd(student);
    }

    @Override
    public Student findId(Integer id) {
        return studentMapper.findId(id);
    }

}

